package com.hundsun.documentripper.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hundsun.documentripper.domain.dto.BaseResponse;
import com.hundsun.documentripper.domain.dto.DocumentTemplateSource;
import com.hundsun.documentripper.domain.pojo.DocumentTemplate;
import com.hundsun.documentripper.domain.query.DocumentTemplateQuery;
import com.hundsun.documentripper.error.ErrorCatcher;
import com.hundsun.documentripper.service.DocumentTemplateService;
import com.hundsun.documentripper.utils.CreateBillCodeUtil;

@Api("文档模板信息")
@RequestMapping("/document_template")
@RestController
public class DocumentTemplateController {

    @Autowired
    private DocumentTemplateService documentTemplateService;

    @ApiOperation("新建文档模板信息")
    @PostMapping("/")
    @ErrorCatcher(code = "8001")
    public BaseResponse<Void> insertDocumentTemplate(@RequestBody DocumentTemplate documentTemplate, HttpServletRequest request) {
        //documentTemplate.setCreateUser((String) request.getSession().getAttribute("userUuid"));
        //生成编码
        documentTemplate.setDocTemplateCode(CreateBillCodeUtil.createCode(CreateBillCodeUtil.DOCUMENT_TEMPLATE_PREFIX, 6));
        documentTemplateService.insertDocumentTemplate(documentTemplate);
        return new BaseResponse<Void>();
    }

    @ApiOperation("更新文档模板信息")
    @PutMapping("/")
    @ErrorCatcher(code = "8002")
    public BaseResponse<Void> updateDocumentTemplate(@RequestBody DocumentTemplate documentTemplate, HttpServletRequest request) {
        //documentTemplate.setModifyUser((String) request.getSession().getAttribute("userUuid"));
        documentTemplateService.updateDocumentTemplate(documentTemplate);
        return new BaseResponse<Void>();
    }

    @ApiOperation("根据唯一标识获取文档模板信息")
    @GetMapping("/{uuid}")
    @ErrorCatcher(code = "8003")
    public BaseResponse<DocumentTemplateSource> getDocumentTemplateById(@PathVariable("uuid") String uuid) {
        return new BaseResponse<DocumentTemplateSource>(documentTemplateService.getDocumentTemplateById(uuid));
    }

    @ApiOperation("文档模板设置节点模板")
    @PostMapping("/set")
    @ErrorCatcher(code = "8004")
    public BaseResponse<DocumentTemplateSource> setNodeTemplate(@RequestBody DocumentTemplateSource documentTemplateSource) {
        return new BaseResponse<DocumentTemplateSource>(documentTemplateService.setNodeTemplate(documentTemplateSource));
    }

    @ApiOperation("删除文档模板信息")
    @DeleteMapping("/{uuid}")
    @ErrorCatcher(code = "8005")
    @Transactional
    public BaseResponse<Void> deleteDocumentTemplate(@PathVariable("uuid") String uuid) {
        documentTemplateService.deleteDocumentTemplate(uuid);
        return new BaseResponse<Void>();
    }

    @ApiOperation("分页获取文档模板信息列表")
    @PostMapping("/list")
    @ErrorCatcher(code = "8006")
    public BaseResponse<IPage<DocumentTemplate>> getDocumentTemplateListByPage(@RequestBody DocumentTemplateQuery baseQuery) {
        return new BaseResponse<IPage<DocumentTemplate>>(documentTemplateService.getDocumentTemplateListByPage(baseQuery));
    }
}
